import errorEvent from '../../errorMessage';
import store from '@/store/index';
const uuid = require('uuid');

export default {
    async getAll() {
        var projectId = store.state.mainOptions.project.projectId;
        var all = `SELECT * FROM project_model WHERE project_id = '${projectId}' AND disable = 0`;
        try {
            let list = await window.DB.find(all);
            return list;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async creat(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var id = uuid.v1();
        var CT = new Date().getTime();
        var sql = `INSERT INTO project_model (
            model_id,
            project_id,
            key,
            name,
            text,
            disable,
            update_time, 
            create_time
            ) VALUES (
            '${id}',
            '${projectId}',
            '${param.key.replace(/\'/g, "\'\'")}',
            '${param.name.replace(/\'/g, "\'\'")}',
            '${param.text.replace(/\'/g, "\'\'")}',
            0,
            ${CT}, 
            ${CT});`;
        try {
            await window.DB.creat(sql);
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async update(param) {
        var CT = new Date().getTime();
        var sql = `
        UPDATE project_model SET 
        key = '${param.key.replace(/\'/g, "\'\'")}',
        name = '${param.name.replace(/\'/g, "\'\'")}',
        text = '${param.text.replace(/\'/g, "\'\'")}',
        update_time = ${CT}
        WHERE model_id = '${param.id}';`;
        try {
            await window.DB.update(sql);
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async delete(id) {
        var CT = new Date().getTime();
        var sql = `
        UPDATE project_model SET 
        disable = 1,
        update_time = ${CT}
        WHERE model_id = '${id}';`;
        try {
            await window.DB.update(sql);
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },


}